package serverside;

import java.awt.Color;
import javax.swing.*;

import java.util.Timer;
import java.util.TimerTask;

public class Main{
    Timer timer;
    Timer timer2;
    JavaApplication1 javaapp1 = new JavaApplication1();
    Music m = new Music();
    String ATMindex = "";
    public String uname;
    public boolean isOn = false;
    class MyTask extends TimerTask {
            public void run() {
                if(javaapp1.jp.b == 1 && javaapp1.jp.u == 0 && javaapp1.check == 0){
                    uname = "";
                    javaapp1.print(uname , false);
                    javaapp1.jp.u = 0;
                    javaapp1.visible();
                    javaapp1.DisplayButton();
                    javaapp1.jp.b = 0;
                    javaapp1.jp.clear();
                }//logged out or startup or back from jp
                if(javaapp1.jp.b == 0 && javaapp1.jp.u == 0 && javaapp1.check == 1)
                {
                    javaapp1.DisplayButton();
                    javaapp1.checkATMs.setVisible(true);
                    javaapp1.check = 0;
                }//button ATMs --- checking ATMs
                if(javaapp1.checkATMs.b == 1 ){
                    javaapp1.jp.u = 1;
                    javaapp1.DisplayButton();
                    javaapp1.checkATMs.setVisible(false);
                    javaapp1.jp.u = 0;
                    javaapp1.checkATMs.b = 0;
                }//back from checkATMs
                /*if(javaapp1.jp.b == 0 && javaapp1.jp.u == 0 && javaapp1.check == 2)
                {
                    javaapp1.ATMconfig.setVisible(true);
                    javaapp1.check = 0;
                    javaapp1.clear();
                    javaapp1.ATMconfig.clearTextboxs();
                }*///button config --- ATMs configuration
                /*if(javaapp1.ATMconfig.b == 1 ){
                    javaapp1.jp.u = 1;
                    javaapp1.ATMconfig.setVisible(false);
                    javaapp1.jp.u = 0;
                    javaapp1.ATMconfig.b = 0;
                    javaapp1.visible();
                }*///back from ATMconfig
                if(javaapp1.jp.u == 1){
                    uname = javaapp1.jp.uname;
                    javaapp1.print(uname , true);
                    javaapp1.DisplayButton();
                    javaapp1.jp.setVisible(false);
                    javaapp1.jp.u = 0;
                }//logged in
                else if (javaapp1.jp.u == 2)
                {
                    uname = javaapp1.jp.uname;
                    javaapp1.print(uname , false);
                    javaapp1.jp.u = 0;
                }//wrong username or password
                //javaapp1.ATMconfig.changID(javaapp1.checkATMs.ind + 1);
                /*if(javaapp1.inc == 1){
                    ATMindex++;
                    javaapp1.checkATMs.Increment();
                    javaapp1.inc = 0;
                }*/
                //JOptionPane.showMessageDialog(null, "Sorry , can't display the ATM " + (javaapp1.checkATMs.lamps.size()) + " " + javaapp1.checkATMs.index);
                if(javaapp1.checkATMs.index == 0){//&& ATMindex >= javaapp1.checkATMs.index){//javaapp1.ATMconfig.ind > 0 && javaapp1.ATMconfig.ind > javaapp1.checkATMs.index){
                    //javaapp1.checkATMs.DisplayATMs(javaapp1.ATMconfig.ind);
                    //javaapp1.checkATMs.DisplayATMs( "sgfd" , "Cairo" , "Shoubrah");
                    javaapp1.checkATMs.DisplayATMs(0);
                }//when there is no displayed atms yet
                if(javaapp1.checkATMs.index < javaapp1.checkATMs.datas.length && javaapp1.checkATMs.index != 0)
                {
                    javaapp1.checkATMs.DisplayATMs(javaapp1.checkATMs.index);
                }//if displayed one atm & there is more not displayed
                //abdoTheking
                for(int i = 0 ; i < Server.onlineATMs.length; i++)
                {
                    for (int j = 0; j < javaapp1.checkATMs.datas.length ; j++) {
                        
                        if (Server.onlineATMs[i] != null) {
                            if(Server.onlineATMs[i].equals(javaapp1.checkATMs.datas[j][0])){
                                javaapp1.checkATMs.lamps.get(j).setBackground(Color.green);
                                isOn = true;
                            }
//                            if(isOn == true && !Server.onlineATMs[i].equals(javaapp1.checkATMs.datas[j][0])){
//                                javaapp1.checkATMs.lamps.get(j).setBackground(Color.red);
//                                isOn = false;
//                            }
                        }
                        
                            
                            
                    }
                }//check if atms on/off
                javaapp1.update();
                //JOptionPane.showMessageDialog(null, "hey" + javaapp1.jp.b);
            }
        }
    /*
    class MyTaskATM extends TimerTask {
        public void run() {
            javaapp1.ATMconfig.changID(javaapp1.checkATMs.index + 1);
        }
    }*/
    
    
    
    
    Main() throws Exception{
        timer = new Timer();
        timer.scheduleAtFixedRate(new MyTask() , 1, 1);
        //timer2 = new Timer();
        //timer2.scheduleAtFixedRate(new MyTaskATM() , 1, 1);
        //m.Music();
        
        javaapp1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
//    public static void main(String[] args) throws Exception{
//        DatabasePack.DatabaseConnection.OpenConnction();
//        Main m = new Main();
//    }
    
     
}